<?php

//ZBP的第一个插件，wordpaster插件

//注册插件
RegisterPlugin("WordPaster", "ActivePlugin_WordPaster");
define('WORDPASTER_PATH', dirname(__FILE__));
define('WORDPASTER_INCPATH', WORDPASTER_PATH . '/inc/');
/** @var Totoro_Class $Totoro */
$WordPaster = null;

function WordPaster_init()
{
    require_once WORDPASTER_PATH . '/inc/wordpaster.php';

    global $WordPaster;
    if (!is_object($WordPaster)) {
        $WordPaster = new WordPaster_Class();
    }
}
$WordPaster_Include_File = array();

function WordPaster_Include($filename)
{
    global $WordPaster_Include_File;
    if (!isset($WordPaster_Include_File[$filename])) {
        $r = include $filename;
        $WordPaster_Include_File[$filename] = $r;
        return $r;
    }
    return $WordPaster_Include_File[$filename];
}

//激活插件
function ActivePlugin_WordPaster()
{
    Add_Filter_Plugin('Filter_Plugin_CSP_Backend', 'wordpaster_UpdateCSP');
    Add_Filter_Plugin('Filter_Plugin_Admin_CommentMng_SubMenu', 'WordPaster_Admin_CommentMng_SubMenu');
    Add_Filter_Plugin('Filter_Plugin_Edit_Begin', 'wordpaster_addscript_begin');
    Add_Filter_Plugin('Filter_Plugin_Edit_End', 'wordpaster_addscript_end');
}

//安装插件
function InstallPlugin_WordPaster()
{
}

//CSP
function wordpaster_UpdateCSP(&$csp)
{
    $urls = " ws: 127.0.0.1 *.ncmem.com wordpaster:";
    $items = array('default-src','connect-src','img-src', 'script-src', 'style-src');
    foreach ($items as $item) {
        if (isset($csp[$item])) {
            $csp[$item] .= $urls;
        }
    }
}

//显示配置按钮
function WordPaster_Admin_CommentMng_SubMenu()
{
    global $zbp;
    echo '<a href="' . $zbp->host . 'zb_users/plugin/WordPaster/main.php"><span class="m-right">WordPaster设置</span></a>';
}

//包含JS，CSS头文件
function wordpaster_addscript_begin()
{

    global $zbp;
    echo '<link type="text/css" rel="Stylesheet" src="' . $zbp->host . 'zb_users/plugin/WordPaster/css/w.css"/>';
    echo '<link type="text/css" rel="Stylesheet" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/layer-3.1.1/theme/default/layer.css"/>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/json2.min.js"></script>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/layer-3.1.1/layer.js"></script>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/w.edge.js"></script>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/w.app.js"></script>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/w.file.js"></script>';
    echo '<script type="text/javascript" src="' . $zbp->host . 'zb_users/plugin/WordPaster/js/w.layer.js"></script>';
}

//写入JS
function wordpaster_addscript_end()
{
    global $zbp;
    WordPaster_init();
    global $WordPaster;
    
    $posturl= $zbp->host . "zb_users/plugin/WordPaster/upload.php";

    $s = <<<js
<script type="text/javascript">

window.UEDITOR_CONFIG['toolbars'][0].push('wordpaster');
window.UEDITOR_CONFIG['toolbars'][0].push('ppt');
window.UEDITOR_CONFIG['toolbars'][0].push('pdf');
window.UEDITOR_CONFIG['toolbars'][0].push('netpaster');
WordPaster.getInstance({
    PostUrl: "{posturl}",
    ImageUrl:"{host}{url}",
    License2:"{lic}"
}).Load();

</script>
js;
    $s = str_replace("{posturl}",$posturl,$s);
    $s = str_replace("{host}",$zbp->host,$s);
    $s = str_replace("{lic}",$WordPaster->config_array["LIC_VALUE"]["VALUE"],$s);
    echo $s;
}
